Imports PTT.CarRes.Data

Public Class ClassQueryInvDataApprove
    Public Function Add(ByVal InvDataApprove As QueryInvDataApprove, ByVal StrConn As String) As Boolean
        Dim ClsInvDataApprove As New ClassInvDataApprove
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvDataApprove.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvDataApprove
            .ApproveBdId = InvDataApprove.ApproveBdId
            .ApproveBd = InvDataApprove.ApproveBd
            .Admin = InvDataApprove.Admin
            .ApproveAgent = InvDataApprove.ApproveAgent
            .MainMenu = InvDataApprove.MainMenu
            .Status = InvDataApprove.Status
            ret = .USP_Save(Conn)
        End With
        ClsInvDataApprove.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Edit(ByVal InvDataApprove As QueryInvDataApprove, ByVal StrConn As String) As Boolean
        Dim ClsInvDataApprove As New ClassInvDataApprove
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvDataApprove.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvDataApprove
            .ApproveBdId = InvDataApprove.ApproveBdId
            .Load(Conn)
            .ApproveBd = InvDataApprove.ApproveBd
            .Admin = InvDataApprove.Admin
            .ApproveAgent = InvDataApprove.ApproveAgent
            .MainMenu = InvDataApprove.MainMenu
            .Status = InvDataApprove.Status
            ret = .USP_Update(Conn)
        End With
        ClsInvDataApprove.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Delete(ByVal InvDataApprove As QueryInvDataApprove, ByVal StrConn As String) As Boolean
        Dim ClsInvDataApprove As New ClassInvDataApprove
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvDataApprove.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvDataApprove
            .ApproveBdId = InvDataApprove.ApproveBdId
            .Load(Conn)
            ret = .USP_Delete(Conn)
        End With
        ClsInvDataApprove.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Search(Optional ByVal ParaApproveBdId As String = "", _
    Optional ByVal ParaStatus As String = "", Optional ByVal StrConn As String = "") As QueryInvDataApproves
        Dim ClsInvDataApprove As New ClassInvDataApprove
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvDataApprove.ConnectionOpen(StrConn)
        Dim ret As QueryInvDataApproves = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvDataApprove"
        If ParaApproveBdId & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(ApproveBdId = " & ParaApproveBdId & ")"
        End If
        If ParaStatus & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(Status like '%" & ParaStatus & "%')"
        End If

        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        End If
        Dim ds As DataSet
        With ClsInvDataApprove
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvDataApproves

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)

                            results.Add(New QueryInvDataApprove( _
                              .Item("ApproveBdId") & "", _
                              .Item("ApproveBd") & "", _
                              .Item("Admin") & "", _
                              .Item("ApproveAgent") & "", _
                              .Item("MainMenu") & "", _
                              .Item("Status") & "" _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvDataApprove.ConnectionClose(Conn)
        Return ret
    End Function


    Public Function SearchCheckAddmin(Optional ByVal UserID As String = "", _
        Optional ByVal StatusAdmin As String = "", Optional ByVal StatusBd As String = "", Optional ByVal StrConn As String = "") As QueryInvDataApproves
        Dim ClsInvDataApprove As New ClassInvDataApprove
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvDataApprove.ConnectionOpen(StrConn)
        Dim ret As QueryInvDataApproves = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvDataApprove"
        If UserID & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(ApproveBdId = " & UserID & ")"
        End If
        If StatusAdmin & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " or "
            End If
            Condition &= "(Admin =0)"
        End If
        If StatusBd & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " or "
            End If
            Condition &= "(ApproveBd =0)"
        End If


        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        End If
        Dim ds As DataSet
        With ClsInvDataApprove
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvDataApproves

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)

                            results.Add(New QueryInvDataApprove( _
                              .Item("ApproveBdId") & "", _
                              .Item("ApproveBd") & "", _
                              .Item("Admin") & "", _
                              .Item("ApproveAgent") & "", _
                              .Item("MainMenu") & "", _
                              .Item("Status") & "" _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvDataApprove.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Load(Optional ByVal ParaApproveBdId As String = "", Optional ByVal StrConn As String = "") As QueryInvDataApproves
        Dim ClsInvDataApprove As New ClassInvDataApprove
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvDataApprove.ConnectionOpen(StrConn)
        Dim ret As QueryInvDataApproves = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvDataApprove"
        If ParaApproveBdId & "" <> "" Then
            If Condition & "" <> "" Then Condition &= " and "
            Condition &= "(ApproveBdId = " & ParaApproveBdId & ")"
        End If

        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        Else
            Return Nothing
            Exit Function
        End If
        Dim ds As DataSet
        With ClsInvDataApprove
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvDataApproves

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)

                            results.Add(New QueryInvDataApprove( _
                              .Item("ApproveBdId") & "", _
                              .Item("ApproveBd") & "", _
                              .Item("Admin") & "", _
                              .Item("ApproveAgent") & "", _
                              .Item("MainMenu") & "", _
                              .Item("Status") & "" _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvDataApprove.ConnectionClose(Conn)
        Return ret
    End Function
End Class